/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.order.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.entity.GoodsVirtual;
import com.wealth.commodity.repository.GoodsDao;
import com.wealth.commodity.repository.GoodsVirtualDao;
import com.wealth.coupon.util.LabelFileEncrypt;
import com.wealth.order.entity.Order;
import com.wealth.order.entity.OrderItem;
import com.wealth.order.entity.OrderVirtual;
import com.wealth.order.entity.VoucherRecord;
import com.wealth.order.enums.OrderStatusEnum;
import com.wealth.order.repository.OrderDao;
import com.wealth.order.repository.OrderVirtualDao;
import com.wealth.order.repository.VoucherRecordDao;
import com.wealth.order.service.IVoucherRecordService;

import iaas.enums.YesOrNoEnum;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class VoucherRecordServiceImpl extends BaseServiceImpl<VoucherRecord> implements IVoucherRecordService{

	@Autowired
	private VoucherRecordDao voucherRecordDao;
	@Autowired
	private GoodsVirtualDao goodsVirtualDao;
	@Autowired
	private OrderVirtualDao orderVirtualDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private OrderDao orderDao;

	/** ---------------- 微信端 ---------------- */
	/** 生成 代金券 */
	@Override
	public void generateVoucherRecord(Order order) throws Exception {
		for (OrderItem item : order.getOrderItemList()) {
			Goods goods = item.getGoods();
			//如果是虚拟商品
			if(goods.getIsVirtual()==YesOrNoEnum.YES.getValue()){
				//生成n张
				for(int i=0;i<item.getAmount();i++){
					saveVoucherRecord(order, goods);
				}
			}
		}
	}
	
	private void saveVoucherRecord(Order order, Goods goods) {
		//生成代金券记录
		GoodsVirtual goodsVirtual =  goodsVirtualDao.findOne(goods.getGoodsVirtual().getId());
		String uid = goodsVirtual.getUid();
		String code  = LabelFileEncrypt.encrypt(uid);
		
		VoucherRecord record = new VoucherRecord();
		record.setMember(order.getMember());
		record.setOrder(order);
		record.setGoods(goods);
		record.setCode(code);
		record.setStatus(YesOrNoEnum.NO.getValue());	//未使用
		voucherRecordDao.save(record);
	}
	
	

	/** 扫描二维码 验证代金券 */
	@Override
	public ResultObject validVoucherRecord(Long memberid,Long merchantid,String code) throws Exception {
		if(StringUtils.isBlank(code)){
			return new ResultObject("500", "代金券序列号不能为空", null);
		}
		
		
		
		//存在记录
		VoucherRecord record =  voucherRecordDao.findByMemberIdAndCode(memberid, code);
		if(record==null){
			return new ResultObject("500", "代金券记录不存在", "");
		}
		
		//判断已经使用过
		if(record.getStatus()==YesOrNoEnum.YES.getValue()){
			return new ResultObject("500", "代金券记录已经使用过", record);
		}
		
		Long GoodsVirtualId=null;
		Date startTime;
		Date endTime;
		String uid = code.substring(0, 4);
		if(record.getGoods()==null){
			//唯一标识
			OrderVirtual orderVirtual =orderVirtualDao.findByUid(uid);
			if(orderVirtual==null){
				return new ResultObject("500", "自提序列号非法", null);
			}
			GoodsVirtualId=orderVirtual.getId();
			startTime=orderVirtual.getStartTime();
			endTime=orderVirtual.getEndTime();
			Order order=orderDao.findByOrderVirtualId(GoodsVirtualId);
			if(order.getMerchant().getId()!=merchantid){
				return new ResultObject("500", "代金券不属于该商户", record);
			}
		}else{
			//唯一标识
			
			GoodsVirtual goodsVirtual =goodsVirtualDao.findByUid(uid);
			if(goodsVirtual==null){
				return new ResultObject("500", "代金券序列号非法", null);
			}
			GoodsVirtualId=goodsVirtual.getId();
			startTime=goodsVirtual.getStartTime();
			endTime=goodsVirtual.getEndTime();
			//判断是否该商户
			Goods goods  = goodsDao.findByGoodsVirtualId(GoodsVirtualId);
			if(goods.getMerchant().getId()!=merchantid){
				return new ResultObject("500", "代金券不属于该商户", record);
			}
		}
		
		
		
		
		
		//间隔
		DateTime start = new DateTime(startTime);
		DateTime end = new DateTime(endTime);
		Interval i = new Interval(start, end);
		if(!i.contains(new DateTime())){
			return new ResultObject("500", "代金券已过期", record);
		}
		
		
		
		
		record.setCreateTime(new Date());
		record.setStatus(YesOrNoEnum.YES.getValue());
		voucherRecordDao.save(record);
		
		//修改订单状态,为已签收
		Order order  = record.getOrder();
		if(order.getStatus()!=OrderStatusEnum.SIGN.getValue()){
			order.setStatus(OrderStatusEnum.SIGN.getValue());
			order.setSignstatus(YesOrNoEnum.YES.getValue());
			order.setSignTime(new Date());
			orderDao.save(order);
		}
		
		return new ResultObject("000", "代金券有效",record);
	}

	@Override
	public void saveVoucherRecord(Order order) {
		//生成代金券记录
		OrderVirtual orderVirtual =  orderVirtualDao.findOne(order.getOrderVirtual().getId());
		String uid = orderVirtual.getUid();
		String code  = LabelFileEncrypt.encrypt(uid);
		VoucherRecord record = new VoucherRecord();
		record.setMember(order.getMember());
		record.setOrder(order);
		record.setCode(code);
		record.setStatus(YesOrNoEnum.NO.getValue());	//未使用
		voucherRecordDao.save(record);
		
	}

	
	
	
}
